package Arrays;

public class _643_MaximumAverageSubarrayI {
    //time limit exceeded
    public double findMaxAverage0(int[] nums, int k) {
        int length = nums.length-k;
        double max = Integer.MIN_VALUE;
        for(int i=0;i<=length;i++) {
            double tempSum = 0;
            for(int j=0;j<k;j++) {
                tempSum+=nums[i+j];
            }
            if(max<tempSum){
                max = tempSum;
            }
        }
        return (max/k);
    }

    //get the sum first and use sub them
    public double findMaxAverage(int[] nums, int k) {
        int[] sum = new int[nums.length];
        sum[0] = nums[0];
        for(int i=1;i<nums.length;i++) {
            sum[i] = sum[i-1] + nums[i];
        }
        double res = sum[k-1];
        for(int i=k;i<nums.length;i++) {
            double temp = sum[i] - sum[i-k];
            res = Math.max(res,temp);
        }
        return res/k;
    }

}
